<?php

namespace App\Http\Repository;

use App\Chunk;

class ChunkRepository
{
    private $chunk;

    public function __construct(Chunk $chunk)
    {
        $this->chunk = $chunk;
    }

    //得到信息模块信息列表
    public function getChunk()
    {
        return $this->chunk
            ->where('status', 1)
            ->lists('name', 'id');
    }

    //得到包含拓展字段的模块列表
    public function getChunkWithExtends($chunk)
    {
        $chunk = $this->chunk
            ->select('chunks.*', \DB::raw('group_concat(line.field) as extends'))
            ->leftjoin(\DB::raw('(SELECT chunk,field FROM chunk_info_field_lines JOIN info_fields WHERE info_fields.id = chunk_info_field_lines.info_field) as line'), 'line.chunk', '=', 'chunks.id')
            ->groupBy('chunks.id')
            ->findorfail($chunk);
        if (!empty($chunk['extends'])) {
            $chunk['extends'] = explode(',', $chunk['extends']);
        } else {
            $chunk['extends'] = [];
        }

        return $chunk;
    }

    //得到指定模块的信息数量
    public function getInfoNumByChunk($chunk)
    {
        $tmpInfoNum = \App\InfoSortLines::where('chunk', $chunk)
            ->join('infos', 'infos.id', '=', 'info_sort_lines.info')
            ->select('infos.id', 'is_push', 'is_new', 'is_hot')
            ->groupBy('infos.id')
            ->get()
            ->toArray();
        $infoNum['num'] = count(array_column($tmpInfoNum, 'id'));
        $infoNum['is_push'] = array_sum(array_column($tmpInfoNum, 'is_push'));
        $infoNum['is_new'] = array_sum(array_column($tmpInfoNum, 'is_new'));
        $infoNum['is_hot'] = array_sum(array_column($tmpInfoNum, 'is_hot'));

        return $infoNum;
    }

    //得到信息模块信息列表
    public function getChunkUserCanWrite()
    {
        return $this->chunk
            ->where('status', 1)
            ->where('user_write', 1)
            ->lists('name', 'id');
    }

    //得到包含拓展字段的模块列表
    public function getChunkWithExtendsUserCanWrite($chunk)
    {
        $chunk = $this->chunk
            ->select('chunks.*', \DB::raw('group_concat(line.field) as extends'))
            ->leftjoin(\DB::raw('(SELECT chunk,field FROM chunk_info_field_lines JOIN info_fields WHERE info_fields.id = chunk_info_field_lines.info_field) as line'), 'line.chunk', '=', 'chunks.id')
            ->groupBy('chunks.id')
            ->where('user_write', 1)
            ->findorfail($chunk);
        if (!empty($chunk['extends'])) {
            $chunk['extends'] = explode(',', $chunk['extends']);
        } else {
            $chunk['extends'] = [];
        }

        return $chunk;
    }

    //得到每个模块的信息数量
    public function getPageViewNumByChunk()
    {
        return $this->chunk
            ->leftjoin('info_sort_lines', 'info_sort_lines.chunk', '=', 'chunks.id')
            ->leftjoin('infos', 'infos.id', '=', 'info_sort_lines.info')
            ->groupBy('chunks.id')
            ->select(\DB::raw('sum(DISTINCT infos.page_view) as num'), 'chunks.name')
            ->get()
            ->toArray();
    }

    //得到指定年份每个模块的信息每月增加数量
    public function getEveryChunkEveryMouthCreateNum($year = '')
    {
        $year = !empty($year) ? $year : date('Y');

        $chunks = $this->chunk
            ->leftjoin('info_sort_lines', 'info_sort_lines.chunk', '=', 'chunks.id')
            ->leftjoin('infos', 'infos.id', '=', 'info_sort_lines.info')
            ->select('infos.created_at as time', 'chunks.name')
            ->groupBy('infos.id')
            ->get()
            ->toArray();

        $data = [];
        foreach ($chunks as $info) {
            $timeArray = explode('-', $info['time']);
            if ($timeArray[0] != $year) {
                continue;
            } else {
                !empty($data[$info['name']][(string)(int)$timeArray[1]]) ? $data[$info['name']][(string)(int)$timeArray[1]]++ : $data[$info['name']][(string)(int)$timeArray[1]] = 1;
            }
        }

        return $data;
    }

    //得到模块信息(包含模版信息)
    public function getChunkInfoWithTheme()
    {
        //得到模块的信息
        $chunkInfo = $this->chunk->get()->toArray();
        //得到单独的模版的信息
        foreach ($chunkInfo as $key => $chunk) {
            $tmpSorts = \App\Sort::where('chunk', $chunk['id'])->lists('theme', 'name')->toArray();
            $tmpSorts['展示页面'] = $chunk['theme'];
            $tmpSorts['详情页面'] = $chunk['info_theme'];
            $chunkInfo[$key]['themes'] = $tmpSorts;
        }
        return $chunkInfo;
    }
}